$import("com.joypard.platform.security.resource.ResourceView");

/**
 * 资源
 */
K.defCtrl("com.joypard.platform.security.resource.ResourceCtrl", {
	resourceView : null,
	resService : null,
	initialize : function() {
		this.resourceView = ResourceView.instance(this);
		// 资源
		E.addListener("updateResEvent", this.onUpdateRes, this);
		E.addListener("delResEvent", this.onDelRes, this);

		// 子资源
		E.addListener("updateChildResEvent", this.onUpdateChildRes, this);
		E.addListener("addChildResEvent", this.onAddChildRes, this);
		E.addListener("delChildResEvent", this.onDelChildRes, this);

		E.addEvents("updateResSuccessEvent");
		E.addEvents("delResSuccessEvent");
	},

	/**
	 * 更新资源信息
	 */
	onUpdateRes : function(form) {
		var input = K.input({
			form : form,
			success : function(text) {
				var output = K.output(text);
				if (!output.showError()) {
					E.fireEvent("updateResSuccessEvent", output);
				}
			}
		});
		K.service(ResourceDI.urls.updateRes, input);
	},
	/**
	 * 删除资源信息
	 */
	onDelRes : function(resNode) {
		var input = K.input({
			params : {
				resIds : resNode.id
			},
			success : function(text) {
				var output = K.output(text);
				if (!output.showError()) {
					E.fireEvent("delResSuccessEvent", output);
				}
			}
		});
		K.service(ResourceDI.urls.delRes, input);
	},
	/**
	 * 添加子资源
	 */
	onAddChildRes : function(form) {
		var input = K.input({
			form : form,
			success : function(text) {
				var output = K.output(text);
				if (!output.showError()) {
					E.fireEvent("addChildResSuccessEvent", output);
				}
			}
		});
		K.service(ResourceDI.urls.addRes, input);
	},
	/**
	 * 删除子资源
	 */
	onDelChildRes : function(records) {
		var input = K.input({
			params : {
				resIds : K.arrayToStr(records, "id")
			},
			success : function(text) {
				var output = K.output(text);
				if (!output.showError()) {
					E.fireEvent("delChildResSuccessEvent", output);
				}
			}
		});
		this.resService.delRes(input);
	}
});
